package com.playwright.util.course;


import com.microsoft.playwright.*;
import com.microsoft.playwright.options.AriaRole;
import com.microsoft.playwright.options.KeyboardModifier;

import java.nio.file.Paths;
import java.util.Arrays;
import java.util.regex.Pattern;

import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;


/**
 * @author liyanhao
 * @Date 2024-01-03 10:03
 */
public class App {
    public static void main(String[] args) {
//        extracted1();
//        extracted1();
//        extracted2();
//        extracted3();
        //生成定位器
//        extracted4();

        System.out.println("执行完毕");

    }

    private static void extracted4() {
        try (Playwright playwright = Playwright.create()) {
            Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
                    .setHeadless(false));
            BrowserContext context = browser.newContext();
            Page page = context.newPage();
            page.navigate("https://demo.playwright.dev/todomvc%E3%80%81");
            page.navigate("https://demo.playwright.dev/todomvc/");
            page.navigate("https://demo.playwright.dev/todomvc/#/");
            page.getByPlaceholder("What needs to be done?").click();
            page.getByPlaceholder("What needs to be done?").fill("feed the dog");
            page.getByPlaceholder("What needs to be done?").press("Enter");
            page.getByRole(AriaRole.CHECKBOX, new Page.GetByRoleOptions().setName("Toggle Todo")).check();
            page.getByRole(AriaRole.CHECKBOX, new Page.GetByRoleOptions().setName("Toggle Todo")).uncheck();
            page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("All")).click();
            page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("Active")).click();
            page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("Completed")).click();
            page.getByRole(AriaRole.HEADING, new Page.GetByRoleOptions().setName("todos")).click();
        }
    }

    private static void extracted3() {
        try (Playwright playwright = Playwright.create()) {
            Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
                    .setHeadless(false));
            BrowserContext context = browser.newContext();
            Page page = context.newPage();
            page.navigate("https://demo.playwright.dev/todomvc/");
            page.navigate("https://demo.playwright.dev/todomvc/#/");
            page.navigate("https://www.baidu.com/");
            page.locator("#kw").click();
            page.locator("#kw").fill("java");
            page.locator("#kw").press("Enter");
            Page page1 = page.waitForPopup(() -> {
                page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("java - 百度翻译 ")).click();
            });
            page1.locator("#desktop-guide-wrapper a").nth(1).click();
            page1.locator("#baidu_translate_input").click();
            page1.locator("#baidu_translate_input").fill("Java se");
            page1.locator("#baidu_translate_input").press("Enter");
            page1.getByText("java软件").first().click();
            page1.getByText("java软件").first().click();
            page1.getByText("java软件").first().click(new Locator.ClickOptions()
                    .setModifiers(Arrays.asList(KeyboardModifier.CONTROL)));
        }
    }

    private static void extracted2() {
        try (Playwright playwright = Playwright.create()) {
            Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(500));
//            Browser browser = playwright.chromium().launch();
            Page page = browser.newPage();
            page.navigate("http://baidu.com");
            System.out.println(page.title());

            // Expect a title "to contain" a substring.
            assertThat(page).hasTitle(Pattern.compile("百度一下，你就知"));

            // create a locator
            Locator getStarted = page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("足迹·2023"));

            // Expect an attribute "to be strictly equal" to the value.
            assertThat(getStarted).hasAttribute("href", "https://www.baidu.com/s?wd=%E8%B6%B3%E8%BF%B9%C2%B72023&sa=fyb_n_homepage&rsv_dl=fyb_n_homepage&from=super&cl=3&tn=baidutop10&fr=top1000&rsv_idx=2&hisfilter=1");

            // Click the get started link.
            getStarted.click();

            // Expects page to have a heading with the name of Installation.
            Locator byRole = page.getByRole(AriaRole.HEADING,
                    new Page.GetByRoleOptions().setName("《从武王伐纣开始建立千年世家》全文免费在线阅读 – 起点..."));
            byRole.click();
            System.out.println();


        }
    }

    private static void extracted1() {
        try (Playwright playwright = Playwright.create()) {
//            Browser launch = playwright.webkit().launch();//无头模式
            Browser launch = playwright.webkit().launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(50));

            Page page = launch.newPage();
            page.navigate("https://baidu.com/");
            page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get("example.png")));
            page.screenshot(new Page.ScreenshotOptions().setPath(
                    Paths.get("D:\\ideaProjects\\JavaTechnologyLearn\\_8106-playwright\\src\\main\\java\\com\\playwright\\course\\demo.png")));
        }
    }

    private static void extracted() {
        try (Playwright playwright = Playwright.create()) {
            Browser browser = playwright.chromium().launch();
            Page page = browser.newPage();
            page.navigate("http://baidu.com");//百度一下，你就知道
            System.out.println(page.title());
        }
    }
}
